<template>
  <view class="container" v-if="!isLoading">
    <mescroll-body ref="mescrollRef" :sticky="true" @init="mescrollInit" :down="{ use: false }" :up="upOption" @up="upCallback">

      <!-- tab栏 -->
      <u-tabs :list="tabList" :is-scroll="false" v-model="curTab" active-color="#786cff" :duration="0.2" @change="onChangeTab" />

      <!-- 团队总人数 -->
      <view class="widget-people f-28 col-9">{{ words.total_team.value }}：{{ teamTotal }}人</view>

      <!-- 列表数据 -->
      <view class="widget-list b-f">
        <view class="widget__detail dis-flex flex-x-between" v-for="(item, index) in list.data" :key="index">
          <view class="detail__left dis-flex flex-y-center">
            <view class="user-avatar">
              <avatar-image :url="item.user.avatar_url" :width="100" :borderWidth="4" :borderColor="`#fff`" />
            </view>
            <view class="user-info dis-flex flex-dir-column flex-x-center">
              <view class="user-nickName f-28">{{ item.user.nick_name }}</view>
              <view class="user-time col-9 f-24">{{ item.create_time }}</view>
            </view>
          </view>
          <view class="detail__right dis-flex flex-dir-column flex-x-center flex-y-center">
            <view class="detail__money">
              <text class="f-24">￥</text>
              <text class="f-34">{{ item.user.expend_money }}</text>
            </view>
            <view class="detail__member f-22" v-if="item.subDealer">
              {{ item.subDealer.first_num + item.subDealer.second_num + item.subDealer.third_num }}个成员
            </view>
          </view>
        </view>
      </view>
    </mescroll-body>
  </view>
</template>

<script>
  import MescrollMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins'
  import AvatarImage from '@/components/avatar-image'
  import { getEmptyPaginateObj, getMoreListData } from '@/core/app'
  import * as Api from '@/api/dealer/team'
  import * as DealerApi from '@/api/dealer'
  import SettingModel from '@/common/model/dealer/Setting'

  const pageSize = 15

  export default {
    components: {
      AvatarImage
    },
    mixins: [MescrollMixin],
    data() {
      return {
        // 选项卡列表
        tabList: [],
        // 当前选项
        curTab: 0,
        // 列表数据
        list: getEmptyPaginateObj(),
        // 上拉加载配置
        upOption: {
          // 首次自动执行
          auto: true,
          // 每页数据的数量; 默认10
          page: { size: pageSize },
          // 数量要大于12条才显示无更多数据
          noMoreSize: 12,
          // 空布局
          empty: {
            tip: '亲，暂无相关数据'
          }
        },
        // 正在加载
        isLoading: true,
        // 文字设置
        words: undefined,
        // 团队总人数
        teamTotal: undefined
      }
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
      const app = this
      app.isLoading = true
      Promise.all([app.getSetting(), app.getDealerUser()])
        .then(result => {
          const setting = result[0]
          const dealer = result[1]
          app.setTabList(setting, dealer)
        })
        .finally(() => app.isLoading = false)
    },

    methods: {

      // 获取分销设置
      getSetting() {
        const app = this
        return new Promise((resolve, reject) => {
          SettingModel.data()
            .then(setting => {
              app.words = setting.words.team.words
              app.setPageTitle(setting)
              resolve(setting)
            })
            .catch(reject)
        })
      },

      // 获取分销用户信息
      getDealerUser() {
        const app = this
        return new Promise((resolve, reject) => {
          DealerApi.user()
            .then(result => resolve(result.data.dealer))
            .catch(reject)
        })
      },

      // 设置页面标题
      setPageTitle(setting) {
        uni.setNavigationBarTitle({
          title: setting.words.team.title.value
        })
      },

      // 设置选项卡数据
      setTabList(setting, dealer) {
        const app = this
        const words = setting.words.team.words
        app.tabList = [
          { value: 1, name: words.first.value, count: dealer.first_num }
        ]
        app.teamTotal = dealer.first_num
        if (setting.basic.level >= 2) {
          app.tabList.push({
            value: 2,
            name: words.second.value,
            count: dealer.second_num
          })
          app.teamTotal += dealer.second_num
        }
        if (setting.basic.level >= 3) {
          app.tabList.push({
            value: 3,
            name: words.third.value,
            count: dealer.third_num
          })
          app.teamTotal += dealer.third_num
        }
      },

      /**
       * 上拉加载的回调 (页面初始化时也会执行一次)
       * 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10
       * @param {Object} page
       */
      upCallback(page) {
        const app = this
        // 设置列表数据
        app.getList(page.num)
          .then(list => {
            const curPageLen = list.data.length
            const totalSize = list.data.total
            app.mescroll.endBySize(curPageLen, totalSize)
          })
          .catch(() => app.mescroll.endErr())
      },

      // 获取提现列表
      getList(pageNo = 1) {
        const app = this
        return new Promise((resolve, reject) => {
          Api.list({ level: app.getTabValue(), page: pageNo })
            .then(result => {
              // 合并新数据
              const newList = result.data.list
              app.list.data = getMoreListData(newList, app.list, pageNo)
              resolve(newList)
            })
        })
      },

      // 获取当前标签项的值
      getTabValue() {
        const app = this
        if (app.tabList.length) {
          return app.tabList[app.curTab].value
        }
        return 1
      },

      // 切换标签项
      onChangeTab(index) {
        const app = this
        // 设置当前选中的标签
        app.curTab = index
        // 刷新订单列表
        app.onRefreshList()
      },

      // 刷新列表数据
      onRefreshList() {
        this.list = getEmptyPaginateObj()
        setTimeout(() => {
          this.mescroll.resetUpScroll()
        }, 120)
      }

    }
  }
</script>

<style>
  page {
    background: #fff;
  }
</style>
<style lang="scss" scoped>
  // 团队人数
  .widget-people {
    padding: 15rpx 25rpx;
    height: 65rpx;
    box-sizing: border-box;
    background: #f7f7f7;
  }

  // 列表内容
  .widget-list {
    padding: 10rpx 20rpx 40rpx 20rpx;
    box-sizing: border-box;
  }

  .widget__detail {
    padding: 20rpx 15rpx;
    box-sizing: border-box;
    font-size: 28rpx;
    border-bottom: 1rpx solid #e7e7e7;

    .user-avatar {
      margin-right: 16rpx;
    }

    .user-info {
      height: 100%;
    }

    .user-nickName {
      margin-bottom: 8rpx;
    }

    .detail__member {
      background-color: #786cff;
      color: #fff;
      padding: 2rpx 15rpx;
      border-radius: 6rpx;
      margin-top: 10rpx;
    }
  }
</style>